package com.dmgameclient;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.dmgameclient.common.URLHelper;
import com.dmgameclient.fragments.FragmentArticle;
import com.dmgameclient.fragments.FragmentForum;
import com.dmgameclient.fragments.FragmentGame;

/**
 * @name 应用程序主页面
 * @Descripation <br>
 *               1、添加7个Fragment到ViewPgaer中，代号0-4为文章，5为论坛，6为游戏。<br>
 *               2、文章模块分类RadioGroup需要与文章的5个Fragment进行同步选中。<br>
 *               2、文章模块分类RadioGroup布局仅限在文章模块中显示，论坛和游戏模块隐藏。<br>
 * @author 樊俊彬
 * @date 2014-9-20
 * @version 1.0
 */
public class MainActivity extends FragmentActivity {

	private static final String TAG = "MainActivity";

	private ViewPager viewPager;
	private ViewPagerAdapter adapter;
	private List<Fragment> fragments = new ArrayList<Fragment>();
	private TextView headerTitle;

	// 文章模块的单选框
	private LinearLayout articleGroupLayout;
	private RadioButton[] articleRadioButtons;
	private int articleCount;
	private int articleCurSel;

	// 主模块的单选框
	private RadioButton[] mainRadioButtons;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_pager_main);
		addSubFragments();
		initViews();
		initFrames();
	}

	/**
	 * 添加Fragments到集合中 5个分类文章、1个论坛、1个游戏
	 */
	private void addSubFragments() {

		Bundle bundle = new Bundle();
		bundle.putString("type", URLHelper.TYPE_ARTICLE_1);
		FragmentArticle articleFragment1 = new FragmentArticle(bundle);

		bundle.putString("type", URLHelper.TYPE_ARTICLE_2);
		FragmentArticle articleFragment2 = new FragmentArticle(bundle);

		bundle.putString("type", URLHelper.TYPE_ARTICLE_3);
		FragmentArticle articleFragment3 = new FragmentArticle(bundle);

		bundle.putString("type", URLHelper.TYPE_ARTICLE_4);
		FragmentArticle articleFragment4 = new FragmentArticle(bundle);

		bundle.putString("type", URLHelper.TYPE_ARTICLE_5);
		FragmentArticle articleFragment5 = new FragmentArticle(bundle);

		FragmentForum forumFragment = new FragmentForum();
		FragmentGame gameFragment = new FragmentGame();

		fragments.add(articleFragment1);
		fragments.add(articleFragment2);
		fragments.add(articleFragment3);
		fragments.add(articleFragment4);
		fragments.add(articleFragment5);
		fragments.add(forumFragment);
		fragments.add(gameFragment);
		articleCount = fragments.size() - 2;
	}

	/**
	 * 加载界面组件
	 */
	private void initViews() {
		// 头部标题和正文ViewPager
		headerTitle = (TextView) findViewById(R.id.headerTitle);
		viewPager = (ViewPager) findViewById(R.id.viewPager);

		// 底部模块单选按钮
		mainRadioButtons = new RadioButton[3];
		mainRadioButtons[0] = (RadioButton) findViewById(R.id.articleButton);
		mainRadioButtons[1] = (RadioButton) findViewById(R.id.forumButton);
		mainRadioButtons[2] = (RadioButton) findViewById(R.id.gameButton);

		// 文章分类单选按钮
		articleGroupLayout = (LinearLayout) findViewById(R.id.radioGroupLinearLayout);
		articleRadioButtons = new RadioButton[articleCount];
		articleRadioButtons[0] = (RadioButton) findViewById(R.id.articleTypeButton_0);
		articleRadioButtons[1] = (RadioButton) findViewById(R.id.articleTypeButton_1);
		articleRadioButtons[2] = (RadioButton) findViewById(R.id.articleTypeButton_2);
		articleRadioButtons[3] = (RadioButton) findViewById(R.id.articleTypeButton_3);
		articleRadioButtons[4] = (RadioButton) findViewById(R.id.articleTypeButton_4);
	}

	/**
	 * 页面的主要逻辑
	 */
	private void initFrames() {

		// 为ViewPager绑定adapter
		adapter = new ViewPagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(adapter);

		// 模块分类单选按钮与ViewPager的同步显示
		mainRadioButtons[0].setTag(0);
		mainRadioButtons[1].setTag(5);
		mainRadioButtons[2].setTag(6);
		for (int i = 0; i < 3; i++) {
			mainRadioButtons[i].setChecked(false);
			mainRadioButtons[i].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int position = (Integer) v.getTag();
					viewPager.setCurrentItem(position);
				}
			});
		}

		// 文章分类单选按钮与ViewPager的同步显示
		for (int i = 0; i < articleCount; i++) {
			articleRadioButtons[i].setTag(i);
			articleRadioButtons[i].setChecked(false);
			articleRadioButtons[i].setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// 标记号
					viewPager.setCurrentItem((Integer) (v.getTag()));
				}
			});
		}

		// 设置第一显示屏

		articleRadioButtons[0].setChecked(true);
		mainRadioButtons[0].setChecked(true);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {

				// 总共有7个Fragment，0-4为文章，5为论坛，6为游戏
				if (position < 5) {
					// 设置头部标题、文章分类单选按钮的选中状态
					articleRadioButtons[articleCurSel].setChecked(false);
					articleRadioButtons[position].setChecked(true);
					articleCurSel = position;
					headerTitle.setText(R.string.article);

					articleGroupLayout.setVisibility(View.VISIBLE);
					mainRadioButtons[0].setChecked(true);
					mainRadioButtons[1].setChecked(false);
					mainRadioButtons[2].setChecked(false);
				} else if (position == 5) {
					articleGroupLayout.setVisibility(View.GONE);
					mainRadioButtons[0].setChecked(false);
					mainRadioButtons[1].setChecked(true);
					mainRadioButtons[2].setChecked(false);
					headerTitle.setText(R.string.forum);
				} else if (position == 6) {
					articleGroupLayout.setVisibility(View.GONE);
					mainRadioButtons[0].setChecked(false);
					mainRadioButtons[1].setChecked(false);
					mainRadioButtons[2].setChecked(true);
					headerTitle.setText(R.string.game);
				}
			}

			@Override
			public void onPageScrolled(int position, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrollStateChanged(int position) {
				// TODO Auto-generated method stub

				// Log.i(TAG, ">>>onPageScrollStateChanged" + position);
			}
		});
		adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * @name ViewPagerAdapter
	 * @Descripation <br>
	 */
	class ViewPagerAdapter extends FragmentPagerAdapter {

		private static final String TAG = "MyPagerAdapter";

		public ViewPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragments.size();
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragments.get(arg0);
		}
	}
}
